import { assign } from 'lodash'
import { defineStore } from 'pinia'
import { useAppStore } from './app.js'

export const useDeviceStore = defineStore('screen',{
	state:()=>{
		return {
			measure:{
				SDKVersion: "",
				appId: "",
				appLanguage: "",
				appName: "",
				appVersion: "",
				appVersionCode: "",
				brand: null,
				browserName: "",
				browserVersion: "",
				deviceBrand: '',
				deviceId: "",
				deviceModel: "",
				deviceOrientation: "",
				devicePixelRatio: 1,
				deviceType: "",
				errMsg: "",
				fontSizeSetting: null,
				hostFontSizeSetting: null,
				hostLanguage: "",
				hostName: "",
				hostPackageName: null,
				hostSDKVersion: null,
				hostTheme: "",
				hostVersion: "",
				language: "",
				model: "",
				osLanguage: null,
				osName: "",
				osTheme: null,
				osVersion: "",
				pixelRatio: 3,
				platform: "",
				safeArea: null,
				safeAreaInsets: null,
				screenHeight: 0,
				screenWidth: 0,
				statusBarHeight: 0,
				system: "",
				ua: "",
				uniCompileVersion: "",
				uniPlatform: "",
				uniRuntimeVersion: "",
				version: "",
				windowBottom: 0,
				windowHeight: 0,
				windowTop: 0,
				windowWidth: 0
			}
		}
	},
	actions:{
		async init()
		{
			let e = uni.getSystemInfoSync()
			
			assign(this.measure,e)
		},
		async statusBarHeight() {
			let statusBarHeight = 
			
			await new Promise((resolve)=>{
				uni.getSystemInfo({
				    success: (e) => {
						console.log(e)
						resolve(e.statusBarHeight)
					}
				})
			})
			
			console.log(statusBarHeight)
			return statusBarHeight
		},
		
		navbarHeight()
		{
			let statusBar = 0  //状态栏高度
			let customBar = 0  // 状态栏高度 + 导航栏高度  
			let navbar = 0 // 自定义标题与胶囊对齐高度
			let navbarHeight = 48
			
			uni.getSystemInfo({
			    success: (e) => {
					// this.compareVersion(e.SDKVersion, '2.5.0')
												
					// #ifdef MP
					statusBar = e.statusBarHeight
					customBar = e.statusBarHeight + 45
					if (e.platform === 'android') {
					  this.$store.commit('SET_SYSTEM_IOSANDROID', false)
					  customBar = e.statusBarHeight + 50
					}
					// #endif
					
					
					// #ifdef MP-WEIXIN
					statusBar = e.statusBarHeight
					const custom = wx.getMenuButtonBoundingClientRect()
					customBar = custom.bottom + custom.top - e.statusBarHeight
									 
					navbar = (custom.top - e.statusBarHeight) * 2 + custom.height
					
					navbarHeight = custom.bottom - custom.top + 10
					// #endif
									 
									 
					// #ifdef MP-ALIPAY
					statusBar = e.statusBarHeight
					customBar = e.statusBarHeight + e.titleBarHeight
					navbarHeight = e.titleBarHeight
					// #endif
									 
									 
					// #ifdef APP-PLUS
					console.log('app-plus', e)
					statusBar = e.statusBarHeight
					customBar = e.statusBarHeight + 45
					// #endif
									 
									 
					// #ifdef H5
					statusBar = 0
					customBar = e.statusBarHeight + 45
					// #endif
				
			    }
			})
		
			return navbarHeight
		},
		
		screenHeight(){
			let screenHeight = 0
			
			uni.getSystemInfo({
			    success: (e) => {
					screenHeight = e.screenHeight
					//console.log(screenHeight)
				}
			})
			
			//console.log(screenHeight)
			return screenHeight
		},
		
		screenWidth(){
			let screenWidth = 0
			
			uni.getSystemInfo({
			    success: (e) => {
					screenWidth = e.screenWidth
					//console.log(screenWidth)
				}
			})
			
			//console.log(screenWidth)
			return screenWidth
		},
		
		windowHeight(){
			let windowHeight = 0
			
			uni.getSystemInfo({
			    success: (e) => {
					windowHeight = e.windowHeight
					//console.log(windowHeight)
				}
			})
			
			//console.log(windowHeight)
			return windowHeight
		},
		
		windowWidth(){
			let windowWidth = 0
			
			uni.getSystemInfo({
			    success: (e) => {
					windowWidth = e.windowWidth
					//console.log(windowWidth)
				}
			})
			
			//console.log(windowWidth)
			return windowWidth
		}
	}
})